﻿using System;
using System.Collections.Generic;
using System.Linq;
using Red.Controls.Diagrams.ViewModels;
using Red.Controls.ViewModels;
using Red.Core.Extensions;
using Red.EngineData.View;

namespace Red.Controls.Diagrams.Actions.InteropActions
{
    public class MoveNodesAction : InteropActionBase
    {
        #region Data

        private IEnumerable<NodeData> _nodesAfterMoveData;

        #endregion Data

        #region Public methods

        public MoveNodesAction( DataItem parentObject, DiagramViewModel diagramViewModel, IEnumerable<Tuple<double, double, string, NodeViewModel>> nodesDataTuple )
        {
            _viewModel = diagramViewModel;
            _parentObject = parentObject;
            _nodesAfterMoveData = nodesDataTuple.Select( item => new NodeData
            {
                NodeX = item.Item4.X,
                NodeY = item.Item4.Y,
                NodeID = item.Item3,
            } ).ToList();
        }

        #endregion Public methods

        #region IAction implementation

        public void Execute()
        {
            MoveNodes( _nodesAfterMoveData );
        }

        #endregion IAction implementation

        #region Private methods

        private void MoveNodes( IEnumerable<NodeData> nodesData )
        {
            var operationID = Guid.NewGuid().ToString();
            foreach ( var nodeData in nodesData )
            {
                _parentObject.Execute( Interop.MakeCall( "moveNode", nodeData.NodeID, nodeData.NodeX, nodeData.NodeY, operationID ) );
            }
        }

        #endregion Private methods
    }
}
